<?php
declare (strict_types=1);

namespace app\api\controller\ucenter;

use app\api\controller\Api;
use app\common\system\logic\DepartmentLogic;

class Department extends Api
{
    public function tree()
    {
        $model = new \app\common\system\model\Department();
        $departments = $model->order("sort desc,id")->column('id,parent_id,title,path,sort', 'id');
        $tree = build_tree([], $departments, 0);

        $result['tree'] = $tree;
        return success($result, '获取数据成功');
    }

    public function getTree()
    {
        $user = $this->request->user;
        $model = new \app\common\system\model\Department();
        if ($user['id'] == 1) {
            $departments = $model->order("sort desc,id")->column('id,parent_id,title,path,sort', 'id');
            $tree = build_tree([], $departments, 0);
        } else {
            // 获取用户所用的部门ID
            $dptLogic = new DepartmentLogic();
            $dtpIds = $dptLogic->getUserDptIds($user['id'], true);
            if (!empty($dtpIds)) {
                // 获取用户所有的部门信息包含path
                $dptObj = $dptLogic->getDepartments($dtpIds, false);
                // 获取用户的根节点
                $departments = $dptLogic->resetDptParentId($dptObj);
                $tree = build_tree([], $departments, 0);
            } else {
                $tree = [];
                $tree = [];
            }
        }
        $result['tree'] = $tree;
        return success($result, '获取数据成功');
    }

    public function getAllDepartment()
    {
        $model = new \app\common\system\model\Department();
        $result['list'] = $model->field('id,title')->order('path')->select();

        return success($result);
    }

    // 创建部门信息
    public function create()
    {
        $post = $this->request->post();
        // 创建的时候清除干扰的信息
        unset($post['id']);

        // 验证提交的数据
        $validate = new \app\common\system\validate\DepartmentValidate();
        if (!$validate->scene('create')->check($post)) return error([], $validate->getError());

        $model = new \app\common\system\model\Department();
        $result = $model->save($post);
        if ($result) {
            if (empty($post['id'])) $post['id'] = $model->id;
            if (!empty($post['parent_id'])) {
                $parent = $model->where('id', $post['parent_id'])->find();
                $path = $parent['path'] . $post['id'] . '-';
                $depth = substr_count($path, '-') - 1;
            } else {
                $path = '0' . '-' . $post['id'] . '-';
                $depth = 1;
            }
            $model->save(['path' => $path, 'depth' => $depth]);
            return success([], '添加部门成功');
        } else {
            return error([], '添加部门失败');
        }

    }

    // 编辑部门信息
    public function save()
    {
        $post = $this->request->post();
        // 验证提交的数据
        $validate = new \app\common\system\validate\DepartmentValidate();
        if (!$validate->scene('edit')->check($post)) return error([], $validate->getError());

        $model = new \app\common\system\model\Department();
        $department = $model->where('id', $post['id'])->find();
        if (!empty($post['parent_id'])) {
            $parent = $model->where('id', $post['parent_id'])->find();
            $path = $parent['path'] . $post['id'] . '-';
            $depth = substr_count($path, '-') - 1;
        } else {
            $path = '0' . '-' . $post['id'] . '-';
            $depth = 1;
        }
        // 更新部门路径及深度
        $post['path'] = $path;
        $post['depth'] = $depth;

        $department->save($post);
        return success([], '编辑部门成功');

    }

    // 删除部门
    public function delete()
    {
        $post = $this->request->post();
        if (empty($post['id'])) return error([], '参数错误');
        // 获取当前删除的部门编号
        $model = new \app\common\system\model\Department();
        $department = $model->where('id', $post['id'])->find();
        if (empty($department)) return error([], '该部门已不存在');

        // 获取当前部门的子部门
        $children = $model->where('parent_id', $post['id'])->find();
        if (empty($children)) {
            $result = $department->delete();
            if ($result) {
                return success([], '删除部门成功');;
            } else {
                return error([], '删除部门失败');
            }
        } else { // 如果存在子部门不能执行删除
            return error([], '请先删除子部门');
        }
    }
}
